﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;


namespace SmsMeta.StructureRequest
{
    public class QueryHelper
    {
        // Special keywords in Parameters used for identifying a resource
        private static string AllKeyword = "all";
        private static string LatestKeyword = "latest";

        // Detail parameter values.
        // This attribute specifies the desired amount of information to be returned. 
        // For example, it is possible to instruct the web service to return only basic information
        // about the maintainable artefact (i.e.: id, agency id, version and name).
        private static string detailFull = "full";
        private static string detailStubs = "allstubs";
        private static string detailRefStubs = "referencestubs";

        // References parameter values
        // This attribute instructs the web service to return (or not) the artefacts referenced by the
        // artefact to be returned (for example, the code lists and concepts used by the data structure
        // definition matching the query)
        private static string refNone = "none";
        private static string refChildren = "children";
        private static string refDescendants = "descendants";

        // Unsupported references
        private static string refParents = "parents";
        private static string refParentsAndSiblings = "parentsandsiblings";
        private static string refAll = "all";

        public  ArtefactRefTyped ArTyped { get; private set; }

        private NameValueCollection query;

        public QueryHelper(ArtefactRefTyped at, NameValueCollection q)
        {
            ArTyped = at;
            query = q;
        }

        /// <summary>
        /// Artefact is perfectly identified
        /// </summary>
        public bool IsSingleArtefact
        {
            get
            {
                return ArTyped.Id != AllKeyword && ArTyped.AgencyId != AllKeyword &&
                    ArTyped.Version != AllKeyword && ArTyped.Version != LatestKeyword;
            }
        }

        public bool IsAllAgencies
        {
            get { return ArTyped.AgencyId == AllKeyword; }
        }

        public bool IsAllId
        {
            get { return ArTyped.Id == AllKeyword; }
        }

        public bool IsUnspecifiedVersion
        {
            get { return ArTyped.Version == AllKeyword || ArTyped.Version == LatestKeyword; }
        }

        public bool IsLatestVersion
        {
            get { return ArTyped.Version == LatestKeyword; }
        }

        public bool IsChildReferencesRequested
        {
            get
            {
                string p = GetQueryValueForParam("references");
                return p == refChildren || p == refDescendants;
            }
        }

        public ArtefactDetail QueryDetail
        {
            get 
            {
                ArtefactDetail a = IsAllAgencies && IsAllId ? ArtefactDetail.AllStubs : ArtefactDetail.Full;
                string p = GetQueryValueForParam("detail");
                if (p == detailStubs)
                    a = ArtefactDetail.AllStubs;
                else if (p == detailRefStubs)
                    a = ArtefactDetail.ReferenceStubs;
                else if (p == detailFull)
                    a = ArtefactDetail.Full;
                return a;
            }
        }


        private string GetQueryValueForParam(string param)
        {
            string val = string.Empty;
            foreach (string s in query.AllKeys)
                if (s == param)
                {
                    val = query[param];
                    break;
                }


            return val;
        }
       
    }
}
